package com.codeyang.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 描述: TO-删除标志-DO
 *
 * @author CodeYang_Site
 * @version 2021/5/18 19:52
 */
@RestController
public class TestController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("add")
    @PreAuthorize("hasAuthority('sys:add')")
    public String add() {
        return "add";
    }


    @GetMapping("del")
    @PreAuthorize("hasAuthority('sys:del')")
    public String del() {
        return "del";
    }


    /**
     * 直接干掉redis中存储的jwt 这样就使得验证无法通过,就完成了登出操作
     *
     * @return
     */
    @PostMapping("myDoLogout")
    public String myDoLogout() {
        // key
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String authorization = request.getHeader("Authorization");
        String jwt = authorization.replaceAll("bearer ", "");
        // 直接干掉redis
        Boolean delete = redisTemplate.delete("jwt:token:" + jwt);
        return delete ? "登出成功" : "登出失败";
    }

}
